﻿using BW.ME.Exam.Domain.Entities;
using BW.ME.Exam.ErrorCode;
using BW.ME.Exam.Read.Api.Applications.Commands.FormQueryCommand;
using BW.ME.Exam.Read.Api.Applications.Commands.UserLoginCommand;
using BW.ME.Exam.Read.Api.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BW.ME.Exam.Read.Api.Controllers
{
    /// <summary>
    /// Api控制器-读
    /// </summary>
    [Route("api/[controller]/[action]")]
    public class ManagementController : ControllerBase
    {
        /// <summary>
        /// 方法
        /// </summary>
        /// <param name="mediator"></param>
        /// <param name="logger"></param>
        public ManagementController(IMediator mediator,ILogger<ManagementController> logger)
        {
            Mediator = mediator;
            Logger = logger;
        }

        public IMediator Mediator { get; }
        public ILogger<ManagementController> Logger { get; }

        /// <summary>
        /// 日志
        /// </summary>
        /// <returns>返回值</returns>
        [HttpGet]
        public IActionResult GetLogTest()
        {
            //异常处理
            try
            {
                Logger.LogInformation("测试方法：调用了一下");
                int num = int.Parse("aa");
            }
            catch (Exception exp)
            {
                Logger.LogError("调用方法报错：" + exp.Message);
                
            }
            return Ok();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="UserPass">密码</param>
        /// <returns>返回值</returns>
        [HttpGet]
        public async Task<ApiResult<UserDto>> LoginQuery(string? UserName ,string? UserPass)
        {
            //异常处理
            try
            {
                Logger.LogInformation("调用了一下登录方法");
                LoginQueryCommand command = new LoginQueryCommand();
                command.UserName = UserName;
                command.UserPass = UserPass;
                return await Mediator.Send(command);
            }
            catch (Exception exp)
            {
                Logger.LogError("调用登录报错：" + exp.Message);
                throw;
            }
        }


        /// <summary>
        /// 获取我的表单列表
        /// </summary>
        /// <param name="CurrentUserId">当前登录人编号</param>
        /// <param name="RoleId">当前登录人角色编号</param>
        /// <param name="CurrentUserNicename">当前登录人昵称</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<InstanceDTO>>> GetMyForms(int CurrentUserId, int RoleId, string? CurrentUserNicename)
        {
            try
            {
                Logger.LogInformation("调用了一下查询方法");
                MyFormQueryCommand command = new MyFormQueryCommand();
                command.CurrentUserId = CurrentUserId;
                command.RoleId = RoleId;
                command.CurrentUserNicename = CurrentUserNicename;
                return await Mediator.Send(command);
            }
            catch (Exception exp)
            {
                Logger.LogError("调用查询报错：" + exp.Message);
                throw;
            }
        }
        /// <summary>
        /// 获取我的待办列表
        /// </summary>
        /// <param name="CurrentUserId"></param>
        /// <returns></returns>

        [HttpGet]
        public async Task<ApiResult<ApiPaging<InstanceDTO>>> GetMyPendings(int CurrentUserId, int index, int size)
        {
            try
            {
                Logger.LogInformation("调用了一下查询方法");
                MyPendingQueryCommand command = new MyPendingQueryCommand();
                command.CurrentUserId = CurrentUserId;
                command.Index = index;
                command.Size = size;
                return await Mediator.Send(command);
            }
            catch (Exception exp)
            {
                Logger.LogError("调用查询报错：" + exp.Message);
                throw;
            }
        }


    }
}
